/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_seek_stub.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
SleSeekStub::SleSeekStub()
{
    memberFuncMap_[SleSeekInterfaceCode::SLE_REGISTER_SEEK_CALLBACK] =
        &SleSeekStub::RegisterSeekCallbackInner;
    memberFuncMap_[SleSeekInterfaceCode::SLE_UNREGISTER_SEEK_CALLBACK] =
        &SleSeekStub::UnregisterSeekCallbackInner;
    memberFuncMap_[SleSeekInterfaceCode::SLE_SEEK_ENABLE] =
        &SleSeekStub::StartSeekInner;
    memberFuncMap_[SleSeekInterfaceCode::SLE_SEEK_DISABLE] =
        &SleSeekStub::StopSeekInner;
    memberFuncMap_[SleSeekInterfaceCode::SLE_GET_DEFAULT_SEEK_CONFIG] =
            &SleSeekStub::GetDefaultSeekParamConfigInner;
}

SleSeekStub::~SleSeekStub(){}

int SleSeekStub::OnRemoteRequest(
    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    SLE_LOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
    std::u16string descriptor = SleSeekStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    SLE_ASSERT_AND_RETURN_RET_LOG(descriptor == remoteDescriptor, ERR_INVALID_STATE, "local descriptor is not equal to remote error!");

    auto itFunc = memberFuncMap_.find((SleSeekInterfaceCode)code);
    if (itFunc != memberFuncMap_.end()) {
        auto memberFunc = itFunc->second;
        if (memberFunc != nullptr) {
            return (this->*memberFunc)(data, reply);
        }
    }
    SLE_LOGW("default case, need check.");
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}

int SleSeekStub::RegisterSeekCallbackInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("RegisterSeekCallbackInner In");
    sptr<IRemoteObject> remote = data.ReadRemoteObject();
    SLE_ASSERT_AND_RETURN_RET_LOG(remote, TRANSACTION_ERR, "read seek remote failed!");
    const sptr<ISleSeekCallback> callback = OHOS::iface_cast<ISleSeekCallback>(remote);
    SLE_ASSERT_AND_RETURN_RET_LOG(callback, TRANSACTION_ERR, "read seek callback failed!");

    int32_t result = RegisterSeekCallback(callback);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleSeekStub::UnregisterSeekCallbackInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("UnregisterSeekCallbackInner In");
    sptr<IRemoteObject> remote = data.ReadRemoteObject();
    SLE_ASSERT_AND_RETURN_RET_LOG(remote, TRANSACTION_ERR, "read seek remote failed!");
    const sptr<ISleSeekCallback> callback = OHOS::iface_cast<ISleSeekCallback>(remote);
    SLE_ASSERT_AND_RETURN_RET_LOG(callback, TRANSACTION_ERR, "read seek callback failed!");

    int result = UnregisterSeekCallback(callback);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleSeekStub::StartSeekInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("StartSeekInner In");
    std::shared_ptr<SeekParam> params(data.ReadParcelable<SeekParam>());
    SLE_ASSERT_AND_RETURN_RET_LOG(params, TRANSACTION_ERR, "read seek param failed!");

    int32_t result = StartSeek(*params);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleSeekStub::StopSeekInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("StopSeekInner In");
    int32_t result = StopSeek();
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleSeekStub::GetDefaultSeekParamConfigInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("GetDefaultSeekParamConfigInner In");
    SleSeekParam seekParam;
    int32_t result = GetDefaultSeekParamConfig(seekParam);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(seekParam.addrType), TRANSACTION_ERR, "write addrType failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(seekParam.repateFilter), TRANSACTION_ERR, "write repateFilter failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(seekParam.filterPolicy), TRANSACTION_ERR, "write filterPolicy failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(seekParam.phys), TRANSACTION_ERR, "write phys failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(seekParam.type[0]), TRANSACTION_ERR, "write type[0] failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(seekParam.interval[0]), TRANSACTION_ERR, "write interval[0] failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(seekParam.window[0]), TRANSACTION_ERR, "write window[0] failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(seekParam.type[1]), TRANSACTION_ERR, "write type[1] failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(seekParam.interval[1]), TRANSACTION_ERR, "write interval[1] failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(seekParam.window[1]), TRANSACTION_ERR, "write window[1] failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(seekParam.type[2]), TRANSACTION_ERR, "write type[2] failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(seekParam.interval[2]), TRANSACTION_ERR, "write interval[2] failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(seekParam.window[2]), TRANSACTION_ERR, "write window[2] failed!");
    return NO_ERROR;
}

} // SleStandard
} // NearLink
} // OHOS